﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Xml.Linq;

namespace RS.ZhaoXiWidgets.Controls
{
    public class ZXBarGraph : ContentControl
    {
        public ZXBarGraph()
        {
           
        }

        static ZXBarGraph()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZXBarGraph), new FrameworkPropertyMetadata(typeof(ZXBarGraph)));
        }




     

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            var actualWidth = this.ActualWidth;
            var actualHeight = this.ActualHeight;


            // 创建一个画笔
            Pen pen = new Pen(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#85ffffff")), 1);
            //drawingContext.DrawLine(pen, new Point(0, 0), new Point(0, actualHeight));
            drawingContext.DrawLine(pen, new Point(0, actualHeight), new Point(actualWidth, actualHeight));




            //pen = new Pen(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#fff")), 1);
            //var stickCount = 13;

            //var stickWidth = actualWidth / (13-1);

            // 创建渐变画刷
            var stops = new GradientStopCollection();
            stops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#2dece8"), 0.0));
            stops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#45a6c7"), 1));


            var gradientBrush = new LinearGradientBrush(stops)
            {
                StartPoint = new Point(0.5, 1),
                EndPoint = new Point(0.5, 0),
                MappingMode = BrushMappingMode.RelativeToBoundingBox
            };


            Pen borderPen = new Pen(new SolidColorBrush((Color)ColorConverter.ConvertFromString("#189781")), 1);

            var gradientPen = new Pen(gradientBrush, 3);
            var yStepDistance = ActualHeight / 5;
            for (int i = 0; i < 6; i++)
            {
                drawingContext.DrawLine(pen, new Point(0, yStepDistance * i), new Point(actualWidth, i * yStepDistance));
            }

            int barCount = 22;
            var barwidth = (actualWidth - 10 * 10 - 11 * 5) / 22;

            int group = 2;
            for (int g = 0; g < group; g++)
            {
                var start = g * barwidth;
                for (int i = 0; i < 11; i++)
                {

                }
            }


            for (int i = 0; i < 22; i++)
            {

                //var x =i / 2D;
                //if (i % 2 == 0)
                //{
                //    drawingContext.DrawRectangle(gradientBrush, borderPen, new Rect((barwidth + 5) * i + (barwidth + 10) * x, 0, barwidth, actualHeight - 1));
                //}
                //else
                //{
                //    drawingContext.DrawRectangle(gradientBrush, borderPen, new Rect((barwidth + 5) * i + (barwidth + 10) * (x - 1), 0, barwidth, actualHeight - 1));
                //}

                if (i % 2 == 0)
                {
                    var x = (barwidth + 5 + barwidth + 10) * i;
                    drawingContext.DrawRectangle(gradientBrush, borderPen, new Rect(x, 0, barwidth, actualHeight - 1));
                }
                else
                {
                    var x = (10 + barwidth + 5) * (i - 1) - 10;
                    //var x = (barwidth + 5)
                    drawingContext.DrawRectangle(gradientBrush, borderPen, new Rect(x, 0, barwidth, actualHeight - 1));
                }
            }

        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
    }
}
